﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MVVM.Core;
using ObjectModel.PowerPoint.New;
using ObjectModel.Excel;

namespace ViewComponents.PowerPoint.ViewModels
{
    public abstract class SourceObjectViewModel : ViewModelBase
    {
        public SourceObject SourceObject { get; private set; }

        public SourceObjectViewModel(SourceObject sourceObject)
        {
            SourceObject = sourceObject;
        }

        //public static SourceObjectViewModel Create(SourceObject sourceObject)
        //{
        //    if (sourceObject is ConstantSource)
        //        return new ConstantSourceObjectViewModel((ConstantSource)sourceObject);
        //    else if (sourceObject is ExcelRangeSourceObject)
        //        return new ExcelSourceObjectViewModel((ExcelRangeSourceObject)sourceObject);
        //    else
        //        return null;
        //}
    }

    public class ConstantSourceObjectViewModel : SourceObjectViewModel
    {
        public System.Collections.ObjectModel.ObservableCollection<Constant> Constants { get; private set; }

        public ConstantSourceObjectViewModel(ConstantSource sourceObject,
            System.Collections.ObjectModel.ObservableCollection<Constant> constants) : base(sourceObject)
        {
            Constants = constants;
            //Microsoft.Office.Interop.PowerPoint.Presentation pres = ObjectModel.PowerPoint.Globals.Application.ActivePresentation;
            //Constants = ObjectModel.PowerPoint.Globals.PresentationConstants[pres].Constants;
        }
    }

    public enum ExcelSourceType { Simple, LookUpSteps }
    
    public class ExcelSourceObjectViewModel : SourceObjectViewModel
    {
        #region Properties

        public ResolvableRange SourceRange
        {
            get { return ((ExcelRangeSourceObject)SourceObject).Range; }
            set { ((ExcelRangeSourceObject)SourceObject).Range = value; }
        }

        public ExcelSourceType SourceRangeType
        {
            get
            {
                return GetSourceRangeType(SourceRange);
            }

            set
            {
                // GET CONFIRMATION.

                SourceRange = CreateSourceRangeFromType(value);
                OnPropertyChanged("SourceRangeType");
            }
        }

        public Array SourceRangeTypes
        {
            get { return Enum.GetValues(typeof(ExcelSourceType)); }
        }

        #endregion

        #region Constructors

        public ExcelSourceObjectViewModel(ExcelRangeSourceObject sourceObject) : base(sourceObject) { }

        #endregion

        #region Methods

        public ResolvableRange CreateSourceRangeFromType(ExcelSourceType type)
        {
            switch (type)
            {
                case ExcelSourceType.Simple:
                    return new ResolvableLiteralRange();

                case ExcelSourceType.LookUpSteps:
                    return new ResolvableLookUpStepRange();

                default:
                    return null;
            }
        }

        public ExcelSourceType GetSourceRangeType(ResolvableRange range)
        {
            if (range is ResolvableLiteralRange)
                return ExcelSourceType.Simple;
            else if (range is ResolvableLookUpStepRange)
                return ExcelSourceType.LookUpSteps;
            else
                throw new InvalidOperationException("Invalid source type.");
        }

        #endregion
    }

    //public class SourceObjectViewModel : ViewModelBase
    //{
    //    SourceObject mTempSourceObject;
    //    public SourceObject TempSourceObject
    //    {
    //        get { return mTempSourceObject; }

    //        set
    //        {
    //            mTempSourceObject = value;
    //            TempSourceObjectType = GetSourceObjectType(value);
    //            OnPropertyChanged("TempSourceObject");
    //        }
    //    }

    //    SourceObjectType? mTempSourceObjectType;
    //    public SourceObjectType? TempSourceObjectType
    //    {
    //        get { return mTempSourceObjectType; }

    //        set
    //        {
    //            mTempSourceObjectType = value;
    //            OnPropertyChanged("TempSourceObjectType");
    //        }
    //    }

    //    SourceObjectType? GetSourceObjectType(SourceObject o)
    //    {
    //        if (o is ConstantSource)
    //            return SourceObjectType.Constant;
    //        else
    //            return null;
    //    }
    //}
}
